package com.yanqu.road.dao.impl.activity.xiuxian;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.xiuxian.data.XiuXianGridPositionData;
import com.yanqu.road.entity.activity.xiuxian.data.XiuXianMissionData;
import com.yanqu.road.entity.activity.xiuxian.data.XiuXianUserMountAllotData;
import com.yanqu.road.entity.activity.xiuxian.data.XiuXianUserMountInfoData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class XiuXianUserMountAllotDataDaoImpl extends TempDataObjectDao<XiuXianUserMountAllotData> {

    public static String getXiuXianUserMountAllotDataSql = "select * from `t_u_activity_xiuxian_mount_allot_data` where `activityId`=?";
    public Map<Long, XiuXianUserMountAllotData> getXiuXianUserMountAllotData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, XiuXianUserMountAllotData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getXiuXianUserMountAllotDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                XiuXianUserMountAllotData data = parse(rs);
                map.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateXiuXianUserMountAllotDataSql = "update `t_u_activity_xiuxian_mount_allot_data` set userMountMap=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(XiuXianUserMountAllotData xiuXianUserMountAllotData) {
        xiuXianUserMountAllotData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(xiuXianUserMountAllotData.getUserMountMap())));
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserMountAllotData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, xiuXianUserMountAllotData.getUserId()));
        if (getDb().executeNoneQuery(updateXiuXianUserMountAllotDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertXiuXianUserMountAllotDataSql = "insert into `t_u_activity_xiuxian_mount_allot_data`(activityId,userId,userMountMap) values (?,?,?)";
    @Override
    protected boolean addData(XiuXianUserMountAllotData xiuXianUserMountAllotData) {
        xiuXianUserMountAllotData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, xiuXianUserMountAllotData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, xiuXianUserMountAllotData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(xiuXianUserMountAllotData.getUserMountMap())));
        if (getDb().executeNoneQuery(insertXiuXianUserMountAllotDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected XiuXianUserMountAllotData parseData(ResultSet rs) throws SQLException {
        XiuXianUserMountAllotData xiuXianUserMountAllotData = new XiuXianUserMountAllotData();
        xiuXianUserMountAllotData.setActivityId(rs.getInt("activityId"));
        xiuXianUserMountAllotData.setUserId(rs.getLong("userId"));
        xiuXianUserMountAllotData.setUserMountMap(JSON.parseObject(rs.getString("userMountMap"), new TypeReference<ConcurrentHashMap<Long, XiuXianUserMountInfoData>>() {
        }));
        return xiuXianUserMountAllotData;
    }
}
